package com.cote.douhahou.pretloan.network;

import static com.cote.douhahou.pretloan.url.AkomapalendPaths.PATH_DELETE_BOUND_BANK_ACCOUNT;
import static com.cote.douhahou.pretloan.url.AkomapalendPaths.PATH_LOAN_LIMIT;
import static com.cote.douhahou.pretloan.url.AkomapalendPaths.PATH_UPLOAD_IMAGE_FILE;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.cote.douhahou.pretloan.any.event.AkomapalendIAnalyticsEvent;
import com.cote.douhahou.pretloan.any.util.AkomapalendAnalyticsUtil;
import com.cote.douhahou.pretloan.launcher.BaseContext;
import com.cote.douhahou.pretloan.utils.LoadingAkomapalendDialog;
import com.cote.douhahou.pretloan.utils.AkomapaLendUtil;
import com.cote.douhahou.pretloan.R;
import com.cote.douhahou.pretloan.beans.AppAkomapalendConfigCustomize;
import com.cote.douhahou.pretloan.beans.ProcessInAkomapalendfo;
import com.cote.douhahou.pretloan.status.RateAkomapalendWindowConfig;
import com.cote.douhahou.pretloan.msg.AccAkomapalendountList;
import com.cote.douhahou.pretloan.beans.LoanApDAkomapalendetailInfo;
import com.cote.douhahou.pretloan.beans.LnLiAkomapalendmitInfo;
import com.cote.douhahou.pretloan.beans.LogAkomapalendinInfo;
import com.cote.douhahou.pretloan.beans.OtheAkomapalendrsInfo;
import com.cote.douhahou.pretloan.beans.PersAkomapalendonalInfo;
import com.cote.douhahou.pretloan.beans.RpIAkomapalendnfo;
import com.cote.douhahou.pretloan.utils.AkomapaLendConfigs;
import com.cote.douhahou.pretloan.url.AkomapalendPaths;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;


public class HttpHelper {

    public interface ResponseLis {
        void onResponse(JSONObject responseJson);
    }

    private static OkHttpClient MyClient;

    public LoadingAkomapalendDialog ldDialog;

    private static final HttpLoggingInterceptor mHttpLogIntetor = new HttpLoggingInterceptor(new HttpAkomapalendLogger());

    private Handler myHandler;

    private HttpHelper() {

        MyClient = new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                .addNetworkInterceptor(mHttpLogIntetor)
                .addInterceptor(new ReqAkomapalendInterceptor())
                .callTimeout(5000, TimeUnit.MILLISECONDS)
                .build();

        myHandler = new Handler();
    }

    private static class InnerC {
        private static final HttpHelper inst = new HttpHelper();
    }

    public static HttpHelper inst() {
        return InnerC.inst;
    }

    public void sLdingDgSafe(FragmentActivity activity) {
        if (activity == null) return;
        postRunSafe(() -> {
            ldDialog = new LoadingAkomapalendDialog(activity);
            ldDialog.show();
        });
    }

    public void dmsLdingDgSafe() {
        if (ldDialog !=null && ldDialog.isShowing()) postRunSafe(() -> ldDialog.dismiss());
    }

    public void toastSafe(CharSequence text) {
        postRunSafe(() -> Toast.makeText(BaseContext.instance(),text,Toast.LENGTH_SHORT).show());
    }

    private void postRunSafe(Runnable runnable){
        if (Looper.myLooper() == Looper.getMainLooper()) runnable.run();
        else myHandler.post(runnable);
    }

    private void requestWithLd(FragmentActivity activity, String body, ResponseLis responseListener) {
        if (AkomapaLendUtil.frontIsEmpty()) return;
        sLdingDgSafe(activity);
        requestNoLd(body,responseListener);
    }

    private void requestNoLd(String body, ResponseLis responseListener) {
        if (AkomapaLendUtil.frontIsEmpty()) return;
        String commonPath = AkomapaLendUtil.commonUrl();
        if (TextUtils.isEmpty(commonPath)) return;
        new Thread(() -> {

            Request request = new Request.Builder()
                    .url(commonPath)
                    .header("product", AkomapaLendConfigs.MyProduct)
                    .post(RequestBody.Companion.create(body, MediaType.Companion.parse("*/*;charset=utf-8")))
                    .build();

            MyClient.newCall(request).enqueue(
                    //响应
                    new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                            toastSafe(BaseContext.instance().getString(R.string.tips_request_fail_akomapalend));
                            dmsLdingDgSafe();
                        }

                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                            String body_src = response.body().string();
                            new Thread(() -> {
                                String body = AkomapaLendUtil.aesDecryptDefault(body_src);
                                if (responseListener!=null) {
                                    try {
                                        JSONObject responseJson = new JSONObject(body);
                                        responseListener.onResponse(responseJson);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                        toastSafe(BaseContext.instance().getString(R.string.tips_request_fail_akomapalend));
                                        dmsLdingDgSafe();
                                    }
                                }
                            }).start();
                        }
                    }
            );

        }).start();
    }

    private String defaultJsonPam(String path, JSONObject jsonObject) {
        if (jsonObject==null) jsonObject = new JSONObject();
        path = path.replaceAll("/", "");
        try {
            jsonObject.put("path", path);
            jsonObject.put("isLimitAdTrackingEnabled", AkomapalendAnalyticsUtil.INSTANCE.adEnabled());
            jsonObject.put("akomapalend1",AkomapaLendConfigs.MyProduct.trim());
            jsonObject.put("androidId", AkomapalendAnalyticsUtil.INSTANCE.androidId());
            jsonObject.put("akomapalendid",AkomapaLendConfigs.AkomapalendCLIENT.trim());
            jsonObject.put("aduid", AkomapalendAnalyticsUtil.INSTANCE.adId());
            jsonObject.put("userId", AkomapaLendUtil.userId().trim());
            jsonObject.put("advertId", AkomapalendAnalyticsUtil.INSTANCE.adId());
            jsonObject.put("akomapalendconfig",AkomapalendAnalyticsUtil.INSTANCE.adId());
            jsonObject.put("aduidPath", AkomapalendAnalyticsUtil.INSTANCE.androidId());
            jsonObject.put("version", AkomapaLendConfigs.AkomapalendVERSION.trim());
            jsonObject.put("token", AkomapaLendUtil.token().trim());
            jsonObject.put("product", AkomapaLendConfigs.MyProduct.trim());
            jsonObject.put("clientType", AkomapaLendConfigs.AkomapalendCLIENT.trim());
            jsonObject.put("apiVersion", "v3");
            jsonObject.put("country", AkomapaLendUtil.pam);
            jsonObject.put("akomapalend_pa",AkomapaLendConfigs.MyProduct.trim());
            jsonObject.put("testakomapalend",AkomapalendAnalyticsUtil.INSTANCE.androidId());
            jsonObject.put("akomapalend",AkomapalendAnalyticsUtil.INSTANCE.adEnabled());
        } catch (JSONException e) {
            e.printStackTrace();
            toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
            dmsLdingDgSafe();
        }
        return AkomapaLendUtil.aesEncrypt(jsonObject.toString());
    }

    public void fetchAppConfig(FragmentActivity activity, Runnable respRunnable) {
        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_APP_SHOW_INFO, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONObject) {
                            AppAkomapalendConfigCustomize.par_akomapalendse(((JSONObject)obj).optString("customize"));
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable != null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                });
    }

    public void fSmsCode(FragmentActivity activity, String phoneNum, Runnable respRunnable) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile", phoneNum.trim());
        } catch (JSONException e) {
            e.printStackTrace();
            toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
            dmsLdingDgSafe();
        }

        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_SMS_CODE, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                       else if (obj instanceof JSONObject) {
                            OtheAkomapalendrsInfo.instance().tSendOtpCodeJson((JSONObject) obj);
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable != null) postRunSafe(respRunnable);
                       }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void lgWCode(FragmentActivity activity, String phoneNum, String verCode, Runnable respRunnable) {
        AkomapaLendUtil.sMobile(phoneNum);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("channelCode", AkomapalendAnalyticsUtil.INSTANCE.installReferrer());
            jsonObject.put("subChannelCode", AkomapalendAnalyticsUtil.INSTANCE.installReferrer());
            jsonObject.put("code", verCode.trim());
            jsonObject.put("mobile", phoneNum.trim());
            jsonObject.put("notBindWalletFlag", "YES");
        } catch (JSONException e) {
            e.printStackTrace();
            toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
            dmsLdingDgSafe();
        }
        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_USER_LOGIN, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONObject) {
                            AkomapalendAnalyticsUtil.INSTANCE.initDevice(BaseContext.instance());
                            LogAkomapalendinInfo.par_akomapalendse((JSONObject) obj);
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void oldUserLg(FragmentActivity activity, String phoneNum, Runnable respRunnable) {
        AkomapaLendUtil.sMobile(phoneNum);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("channelCode", AkomapalendAnalyticsUtil.INSTANCE.installReferrer());
            jsonObject.put("subChannelCode", AkomapalendAnalyticsUtil.INSTANCE.installReferrer());
            jsonObject.put("code", "0000");
            jsonObject.put("mobile", phoneNum.trim());
            jsonObject.put("directLoginFlag","YES");
            jsonObject.put("notBindWalletFlag","YES");
        } catch (JSONException e) {
            e.printStackTrace();
            toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
            dmsLdingDgSafe();
        }
        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_USER_LOGIN, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONObject) {
                            AkomapalendAnalyticsUtil.INSTANCE.initDevice(BaseContext.instance());
                            LogAkomapalendinInfo.par_akomapalendse((JSONObject) obj);
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void submitFeedback(FragmentActivity activity, String type, String opinion, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("type", type);
            jsonObject.put("opinion", opinion);
        } catch (JSONException e) {
            e.printStackTrace();
            toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
            dmsLdingDgSafe();
        }
        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_FEED_BACK, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONObject) {
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void process(FragmentActivity activity, Runnable respRunnable) {
        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_USER_PROCESS, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONObject) {
                            ProcessInAkomapalendfo.parse_akomapalend((JSONObject) obj);
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void processSubmit(FragmentActivity activity, String path, TreeMap<String, String> dataMap, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            for (String k : dataMap.keySet()) {
                String value = dataMap.get(k);
                jsonObject.put(k, value);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
            dmsLdingDgSafe();
        }

        requestWithLd(
                activity,
                defaultJsonPam(path, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONObject) {
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                            //经验+5
                            PersAkomapalendonalInfo.instance().addExpValue(5);
                            PersAkomapalendonalInfo.instance().uploadUserInfo(null,null);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void homeAmounts(FragmentActivity activity, Runnable respRunnable) {
        requestWithLd(
                activity,
                defaultJsonPam(PATH_LOAN_LIMIT, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONObject) {
                            LnLiAkomapalendmitInfo.parse_akomapalend((JSONObject) obj);
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void applyDetails(FragmentActivity activity, Runnable respRunnable) {
        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_GET_LOAN_APPLY_DETAIL, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONObject) {
                            LoanApDAkomapalendetailInfo.parse_akomapalend((JSONObject) obj);
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void startApply(FragmentActivity activity, Runnable respRunnable) {
        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_LOAN_APPLY, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }

                        else if (obj instanceof JSONObject) {
                            AkomapalendAnalyticsUtil.INSTANCE.addEvent(AkomapalendIAnalyticsEvent.AT_LOAN_APPLY);
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void fRepayDetail(FragmentActivity activity, Runnable respRunnable) {
        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_REPAY_INFO, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONObject) {
                            RpIAkomapalendnfo.parse((JSONObject) obj);
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void repayPath1(FragmentActivity activity, String deferredFlag, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("deferredFlag", deferredFlag.trim());
        } catch (JSONException e) {
            e.printStackTrace();
            toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
            dmsLdingDgSafe();
        }
        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_ORDER_REPAY_H5, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONObject) {
                            OtheAkomapalendrsInfo.instance().sRepayPath(((JSONObject)obj).optString("url"));
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void repayPath2(FragmentActivity activity, String deferredFlag, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("deferredFlag", deferredFlag.trim());
        } catch (JSONException e) {
            e.printStackTrace();
            toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
            dmsLdingDgSafe();
        }
        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_GET_ORDER_REPAY_H5, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONObject) {
                            OtheAkomapalendrsInfo.instance().sRepayPath(((JSONObject)obj).optString("repayUrl"));
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void fAccountList(FragmentActivity activity, Runnable respRunnable) {
        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_FETCH_BOUND_BANK_ACCOUNT, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONArray) {
                            AccAkomapalendountList.parse_akomapalend((JSONArray) obj);
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void newAccount(FragmentActivity activity, String walletMobile, String walletMobileConfirm, String walletChannel, Runnable respRunnable, boolean addBindFlag) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("walletMobile", walletMobile);
            jsonObject.put("walletMobileConfirm", walletMobileConfirm);
            jsonObject.put("walletChannel", walletChannel);
            jsonObject.put("addBindFlag", addBindFlag ? "YES":"NO");
        } catch (JSONException e) {
            e.printStackTrace();
            toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
            dmsLdingDgSafe();
        }
        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_BIND_BANK_ACCOUNT, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONObject) {
                            JSONObject json = (JSONObject) obj;
                            OtheAkomapalendrsInfo.instance().sChannel(json.optString("channel"));
                            OtheAkomapalendrsInfo.instance().stAddBindFlag(json.optString("addBindFlag"));
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void removeAccount(FragmentActivity activity, String ids, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("ids", ids);
        } catch (JSONException e) {
            e.printStackTrace();
            toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
            dmsLdingDgSafe();
        }
        requestWithLd(
                activity,
                defaultJsonPam(PATH_DELETE_BOUND_BANK_ACCOUNT, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONObject) {
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void fRateConfig(FragmentActivity activity, Runnable respRunnable){
        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_FETCH_STAR_WINDOW_CONFIG, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0 && !TextUtils.isEmpty(msg)) {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                        
                        else if (obj instanceof JSONObject) {
                            RateAkomapalendWindowConfig.parseData_akomapalend((JSONObject) obj);
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void submitEvent(String page, String event, String remark) {
        if (AkomapaLendUtil.frontIsEmpty()||TextUtils.isEmpty(page)) return;
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("type", AkomapaLendConfigs.MyProduct +"_"+page+"_"+event);
            if (!TextUtils.isEmpty(remark)) {
                jsonObject.put("remark",remark);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
            dmsLdingDgSafe();
        }

        if (!AkomapaLendUtil.frontIsEmpty()){
            String commonPath = AkomapaLendUtil.commonUrl();
            if (TextUtils.isEmpty(commonPath)) return;
            new Thread(() -> {

                Request request = new Request.Builder()
                        .url(commonPath)
                        .header("product", AkomapaLendConfigs.MyProduct)
                        .post(RequestBody.Companion.create(defaultJsonPam(AkomapalendPaths.PATH_USER_BURIED_POINT, jsonObject), MediaType.Companion.parse("*/*;charset=utf-8")))
                        .build();

                MyClient.newCall(request).enqueue(
                        //响应
                        new Callback() {
                            @Override
                            public void onFailure(@NonNull Call call, @NonNull IOException e) {}
                            @Override
                            public void onResponse(@NonNull Call call, @NonNull Response response) {}
                        }
                );

            }).start();
        }
    }

    public void fCountryConfig(FragmentActivity activity, String timeZ, Runnable respRunnable){

        sLdingDgSafe(activity);

        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("p", AkomapaLendConfigs.MyProduct.toLowerCase());
            jsonObject.put("tz",timeZ);
        }catch (JSONException e){
            e.printStackTrace();
            toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
            dmsLdingDgSafe();
        }

        String body = AkomapaLendUtil.aesEncrypt(jsonObject.toString());

        new Thread(() -> {
            MediaType mediaType = MediaType.Companion.parse("*/*;charset=utf-8");
            Request request = new Request.Builder()
                    .url(AkomapalendPaths.TimezonePath)
                    .header("product", AkomapaLendConfigs.MyProduct)
                    .post(RequestBody.Companion.create(body, mediaType))
                    .build();
            Call call = MyClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    toastSafe(BaseContext.instance().getString(R.string.tips_request_fail_akomapalend));
                    dmsLdingDgSafe();
                }

                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                    String body = AkomapaLendUtil.aesDecryptDefault(Objects.requireNonNull(response.body()).string());
                    try {
                        JSONObject responseJson = new JSONObject(body);
                        dmsLdingDgSafe();
                        String msg = responseJson.optString("msg");
                        if (msg.equalsIgnoreCase("success")){
                            String[] data = responseJson.optString("data").split(",");
                            String front = "";
                            String mobileprefix = "";
                            String pa = "";
                            if (data.length>0) front = data[0];
                            if (data.length>1) mobileprefix = data[1];
                            if (data.length>2) AkomapaLendUtil.pam = data[2];
                            AkomapaLendUtil.frontFormat(front);
                            AkomapaLendUtil.mobileCode(mobileprefix);

                            dmsLdingDgSafe();
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else {
                            dmsLdingDgSafe();
                            toastSafe(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
                        dmsLdingDgSafe();
                    }
                }
            });
        }).start();
    }

    public void saveImage(FragmentActivity activity, String filePath_equitable, Runnable respRunnable) {
        sLdingDgSafe(activity);
        try {
            if (!TextUtils.isEmpty(filePath_equitable)) {
                String fileSuffix_equitable = filePath_equitable.endsWith(".png") ? "image/png" : "image/jpg";
                final MediaType mediaType_equitable = MediaType.parse(fileSuffix_equitable);
                final File imageFile_equitable = new File(filePath_equitable);

                final String uplodPaths_equitable = AkomapalendPaths.BasePath + AkomapaLendUtil.front()+"/"+ PATH_UPLOAD_IMAGE_FILE;
                if (imageFile_equitable.isFile()) {
                    RequestBody imageBody = RequestBody.create(mediaType_equitable, imageFile_equitable);

                    RequestBody requestBody_equitable = new MultipartBody.Builder()
                            .setType(MultipartBody.FORM)
                            .addFormDataPart("image", imageFile_equitable.getName(), imageBody)
                            .build();

                    Request request_equitable = new Request.Builder()
                            .url(uplodPaths_equitable)
                            .post(requestBody_equitable)
                            .build();
                    Call call = MyClient.newCall(request_equitable);
                    //响应
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                            toastSafe(BaseContext.instance().getString(R.string.tips_request_fail_akomapalend));
                            dmsLdingDgSafe();
                        }

                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                            String body = Objects.requireNonNull(response.body()).string();
                            try {
                                JSONObject responseJson = new JSONObject(body);
                                int code = Integer.parseInt(responseJson.optString("code"));
                                String msg = responseJson.optString("msg");
                                Object obj = responseJson.opt("obj");
                                
                                if (code!=0) {
                                    toastSafe(msg);
                                    return;
                                }
                                
                                if (obj instanceof JSONObject) {
                                    OtheAkomapalendrsInfo.instance().sImgPath(((JSONObject)obj).optString("url"));
                                    dmsLdingDgSafe();
                                    //...
                                    if (respRunnable!=null) postRunSafe(respRunnable);
                                }else dmsLdingDgSafe();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void saveUserData(String jsonStr) {
        if (AkomapaLendUtil.frontIsEmpty()) return;
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userInfo",jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
            toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
            dmsLdingDgSafe();
        }

        if (!AkomapaLendUtil.frontIsEmpty()){
            String commonPath = AkomapaLendUtil.commonUrl();
            if (TextUtils.isEmpty(commonPath)) return;
            new Thread(() -> {

                Request request = new Request.Builder()
                        .url(commonPath)
                        .header("product", AkomapaLendConfigs.MyProduct)
                        .post(RequestBody.Companion.create(defaultJsonPam(AkomapalendPaths.PATH_SAVE_DATA, jsonObject), MediaType.Companion.parse("*/*;charset=utf-8")))
                        .build();

                MyClient.newCall(request).enqueue(
                        //响应
                        new Callback() {
                            @Override
                            public void onFailure(@NonNull Call call, @NonNull IOException e) {}
                            @Override
                            public void onResponse(@NonNull Call call, @NonNull Response response) {}
                        }
                );

            }).start();
        }
    }

    public void saveUserData(String jsonStr, FragmentActivity activity, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userInfo",jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
            toastSafe(BaseContext.instance().getString(R.string.tips_error_akomapalend));
            dmsLdingDgSafe();
        }

        requestWithLd(
                activity,
                defaultJsonPam(AkomapalendPaths.PATH_SAVE_DATA, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                        }
                        
                        else if (obj instanceof JSONObject) {
                            dmsLdingDgSafe();
                            //...
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void fUSerData(Runnable respRunnable){
        requestNoLd(
                defaultJsonPam(AkomapalendPaths.PATH_GET_DATA, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        
                        if (code!=0) {
                            dmsLdingDgSafe();
                        }
                        
                        else if (obj instanceof JSONObject) {
                            RateAkomapalendWindowConfig.parseData_akomapalend((JSONObject) obj);
                            dmsLdingDgSafe();
                            //...
                            PersAkomapalendonalInfo.parse_akomapalend(((JSONObject)obj).optString("userInfo"));
                            if (respRunnable!=null) postRunSafe(respRunnable);
                        }else dmsLdingDgSafe();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

}
